﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Entry;
using Cofe.Core.EntryType;
using Cofe.Core.Implements;
using Cofe.Core.Property;
using Cofe.Data.Utils;

namespace Cofe.Data
{

    //public class CofeDBEntry
    //{
    //    public Guid Id { get; set; }
    //    public List<CofeSystemInfo> Implementations { get; set; }
    //}

    public class CofeDbContext : DbContext
    {
        #region Constructor

        public CofeDbContext()
        {
            this.Configuration.AutoDetectChangesEnabled = false;
           
        }

        public class Initializer : MigrateDatabaseToLatestVersion<CofeDbContext, Cofe.Data.Migrations.Configuration>
        {
            public Initializer(bool alwaysCreateNewDatabase = true)
            {
                AlwaysCreateNewDatabase = alwaysCreateNewDatabase;
            }

            public bool AlwaysCreateNewDatabase { get; private set; }

            public void InitializeDatabase(CofeDbContext context)
            {
                if (AlwaysCreateNewDatabase || !context.Database.Exists() || !context.Database.CompatibleWithModel(false))
                {
                    context.Database.Delete();
                    context.Database.Create();

                    foreach (var config in CofeServices.ServiceLocater.FindAllServices<IDatabaseConfigurator>())
                        config.OnDatabaseCreating(context);
                }
            }


        }

        #endregion

        #region Methods

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            if (CofeServices.ServiceLocater == null)
                CofeServices.ImportServices(new MEFServiceImporter());
            //If you crash here during migration please uncomment the above line
            foreach (var config in CofeServices.ServiceLocater.FindAllServices<IDatabaseConfigurator>())
                config.OnModelCreating(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }


        public override int SaveChanges()
        {
            foreach (var relationship in this.GetAddedRelationships())
            {
                Console.WriteLine(
                    "Relationship added between {0} and {1}",
                    relationship.Item1,
                    relationship.Item2);
            }

            foreach (var relationship in this.GetDeletedRelationships())
            {
                Console.WriteLine(
                    "Relationship removed between {0} and {1}",
                    relationship.Item1,
                    relationship.Item2);
            }

            return base.SaveChanges();
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        public DbSet<DatabaseEntry> DBEntries { get; set; }
        public DbSet<CofeSystemInfo> Entries { get; set; }
        public DbSet<HashTag> HashTags { get; set; }
        public DbSet<CofeSystemEventLog> CofeSystemEventLogs { get; set; }
        public DbSet<DBVolumeInfo> Volumes { get; set; }

        #endregion


    }
}
